from midpoint import Midpoint
from voronoi import Voronoi

class Heightmap(object):
    """ Class combining the two diagrams (the results of midpoint and Voronoi) 
        into the one height map. The combination is done by multiplying 
        and adding the values.
        
        class attribs:
        _map - the reference to the map created via midpoint 
            (also the reference to the final map after merging the two diagrams together)
        _mapTemp - the reference to the map created via Voronoi
        _size - the size of each map (both Voronoi and midpoint diagrams must 
            be of the same size)
        _maxHeightForMidpoint - the maximum (initial) height for midpoint diagram
        
        _coeff1 - the coefficient for multiplying Voronoi map values
        _coeff2 - the coefficient substracted from midpoint map values
        
        _midpoint - reference to the object of the class Midpoint
        _voronoi - reference to the object of the class Voronoi
    """
    
    _map = None
    _mapTemp = None
    
    _size = 0
    _maxHeightForMidpoint = 0
    
    _coeff1 = 0.01
    _coeff2 = 0
    
    _midpoint = None
    _voronoi = None
    
    def __init__(self, iSize = 100, iMaxHeightForMidpoint = 256):
        """ Init. It creates new instances of classes Midpoint and Voronoi,
            and assigns them to their references (_midpoint, _voronoi). It also
            sets the size of the grid and 'maximum value' for midpoint.
            
            params:
            iSize - the size of the grid (by default it's 100)
            iMaxHeightForMidpoint - the 'maximum value' for midpoint algorithm 
            (by defaut it's 256)
        """
        
        self._size = iSize
        self._maxHeightForMidpoint = iMaxHeightForMidpoint
        
        self._midpoint = Midpoint(self._size, self._maxHeightForMidpoint)
        self._voronoi = Voronoi(self._size)
        

    def createNewHeightmap(self, iSize = None):
        """ The method used to create the new elevation map.
            It creates two maps out of Midpoint and Voronoi classes'
            instances, and then combines them.
            It also assigns the new size of the grid (if the number is given; 
            in the other case it uses the size that has been set previously)
            
            params:
            iSize - (optional) the size of the grid
        """
        if iSize is not None:
            self._size = iSize
        
        self._map = self._midpoint.createNewDiagram(self._size)
        self._mapTemp = self._voronoi.createNewDiagram(self._size)
        
        for i in xrange(self._size):
            for j in xrange(self._size):
                self._map[i][j] = int((self._map[i][j] - self._coeff2) + (self._mapTemp[i][j]) * self._coeff1)
                
        return self._map
    
    
#### the code below is just for testing, it's not the essential part of the class!
#h = Heightmap()
#map = h.createNewHeightmap()
#
#import matplotlib.pyplot as plt
##
#plt.imshow(map)
#plt.show()
